<template>
  <div class="box">
    <div class="content">
      <h1>欢迎来到商家管理系统</h1>
      <p>我们拥有专业团队为您服务</p>
      <p>期待您成为我们中的一员</p>
      <p class="linkTag" @click="register">注册成为商家</p>
      <p class="linkTag" @click="login">我已是商家，我要登录</p>
    </div>
    <div class="register-box" v-if="state=='1'">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="register-form"
      >
      <i class="el-icon-close" @click="closeWindow"></i>
        <h3>注册</h3>
        <el-form-item label="账号" prop="username">
          <el-input
            type="username"
            v-model="ruleForm.username"
            autocomplete="off"
            placeholder="请输入你要注册的账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="littlename">
          <el-input
            type="name"
            v-model="ruleForm.littlename"
            autocomplete="off"
            placeholder="请输入你的称呼"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass" class="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
            show-password
            placeholder="请输入你的账号密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
            show-password
            placeholder="请再次输入你的密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="register-box" v-else-if="state=='2'">

      <el-form
        :model="ruleForm2"
        status-icon
        :rules="rules"
        ref="ruleForm2"
        label-width="100px"
        class="register-form"
      >
        <i class="el-icon-close" @click="closeWindow"></i>
        <h3>登录</h3>
        <el-form-item label="账号" prop="login_username">
          <el-input
            v-model="ruleForm2.login_username"
            autocomplete="off"
            placeholder="请输入你的账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="login_pass">
          <el-input
            type="password"
            v-model="ruleForm2.login_pass"
            autocomplete="off"
            show-password
            placeholder="请输入你的账号密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm2('ruleForm2')">登录</el-button>
          <el-button @click="resetForm('ruleForm2')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    var validateUserName = async (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        let result = await this.usernameRepeat(value);
        if (result) {
          callback(new Error("用户已存在"));
        } else {
          callback();
        }
      }
    };
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入昵称"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var validateLoginUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validateLoginPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        littlename: "",
        pass: "",
        checkPass: "",
        state: false
      },
      state: "0",
      ruleForm2: {
        login_username: "",
        login_pass: ""
      },
      rules: {
        pass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{8,20}$/,
            message:
              "密码为数字，小写字母，大写字母，特殊符号 至少包含三种，长度为 8 - 20位"
          }
        ],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        littlename: [{ validator: validateName, trigger: "blur" }],
        username: [{ validator: validateUserName, trigger: "blur" }],
        login_username: [{ validator: validateLoginUsername, trigger: "blur" }],
        login_pass: [{ validator: validateLoginPass, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios.post("/merchants/add", this.ruleForm).then(({ data }) => {
            if (data) {
              console.log(this.ruleForm);
              alert("注册成功!");
              this.state='0';
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    submitForm2(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios.post("/merchants/login", this.ruleForm2).then(({ data }) => {
            if (data.success) {
              localStorage.token=data.token;
              alert("登录成功!");
              this.state='0';
              sessionStorage.setItem('userName',this.ruleForm2.login_username);
              this.$router.push({
                path:'/merchant'
              })

            }else{
              alert("登录失败！");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    async usernameRepeat(username) {
      const { data } = await axios.post("/merchants/repeat", {
        username
      });
      return data;
    },
    closeWindow() {
      this.state = "0";
    },
    register() {
      this.state = "1";
    },
    login() {
      this.state = "2";
    }
  }
};
</script>

<style scoped>
.box {
  background: url("./几何商务.jpg") no-repeat;
  background-size: cover;
  width: 100%;
  height: 900px;
  position: relative;
}
.register-box {
  position: relative;
  width: 100%;
  height: 900px;
  background-color: rgba(96, 96, 96, 0.5);
  z-index: 99;
}
.register-form {
  position: absolute;
  left: 30%;
  top: 20%;
  width: 30%;
  height: 300px;
}
.register-box .el-icon-close {
  position: absolute;
  right: 0px;
  font-size: 30px;
}
.register-form h3 {
  text-align: center;
  color: #409eff;
}
.register-form .pass {
  margin-bottom: 40px;
}
.content {
  position: absolute;
  z-index: 1;
  right: 20px;
  top: 60px;
}
.linkTag{
  cursor: pointer;
  color: #409eff;
}
</style>